package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
  @Autowired
    private UserService userService;

    /**
     * 查询用户
     *
     */
    @GetMapping
  public Result query(UserDto userDto){
      log.info("查询所有用户{}",userDto);
      PageResult<User> pageResult = userService.query(userDto);
      return Result.success(pageResult);
  }
    /**
     * 新增用户
     */
    @PostMapping
    public Result add(@RequestBody User user){
        log.info("新增用户{}",user);
        userService.add(user);
        return Result.success();
    }
    /**
     * 批量删除用户
     */
    @DeleteMapping ("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除用户{}", ids);
        userService.delete(ids);
        return Result.success();
    }
    /**
     * 查询用户,根据ID回溯
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询用户{}", id);
        User user = userService.selectById(id);
        return Result.success(user);
    }
    /**
     * 修改用户
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("修改用户{}", user);
        userService.updateUser(user);
        return Result.success();
    }
    /**
     * 查询所有用户
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有用户");
        List<User> list = userService.findAll();
        return Result.success(list);
    }
    /**
     * 根据角色查询用户
     */
    @GetMapping("/role/{roleLabel}")
    public Result queryByRole(@PathVariable String roleLabel) {
        log.info("根据角色查询用户{}", roleLabel);
        List<User> list = userService.queryByRole(roleLabel);
        return Result.success(list);
    }
    /**
     * 根据部门查询角色
     */
    @GetMapping("/dept/{deptId}")
    public Result queryByDept(@PathVariable Integer deptId) {
        log.info("根据部门查询角色{}", deptId);
        List<User> list = userService.queryByDept(deptId);
        return Result.success(list);
    }

}
